package com.visionork.android.twe.activity;

import java.util.ArrayList;
import java.util.List;

import com.visionork.android.twe.RepositoryKey;
import com.visionork.android.twe.TWEApplication;
import com.visionork.android.twe.model.Program;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class FavoriteProgramActivity extends ListActivity {
	protected class FavoriteProgramAdapter extends ArrayAdapter<Program> {
		private ArrayList<Program> items;

		public FavoriteProgramAdapter(Context context, int textViewResourceId,
				ArrayList<Program> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Resources res = getResources();
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.program_list_item, null);
			}

			Program prog = items.get(position);
			if (prog != null) {
				TextView tvTitle = (TextView) v.findViewById(R.id.title);
				TextView tvHosts = (TextView) v.findViewById(R.id.hosts);
				if (tvTitle != null) {
					tvTitle.setText(prog.getTitle());
				}

				String hosts = prog.getHosts();
				if (tvHosts != null && hosts != null
						&& hosts.trim().length() > 0) {
					tvHosts.setText(res.getString(R.string.hosts) + " : "
							+ hosts);
				}
			}
			return v;
		}
	}

	protected class LoadFavoriteProgramTask extends AsyncTask<Void, Void, Void> {
		protected ProgressDialog pd;
		private ArrayList<Program> programs;

		@Override
		protected Void doInBackground(Void... params) {
			try {
				programs = new ArrayList<Program>();

				ArrayList<Program> tmpPrograms = TWEApplication.getRepository(
						RepositoryKey.ShowRepository).getPrograms();

				for (Program prog : tmpPrograms) {
					if (prog.isFavorite()) {
						programs.add(prog);
					}
				}
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			pd = ProgressDialog.show(FavoriteProgramActivity.this,
					res.getString(R.string.d_loading),
					res.getString(R.string.d_loading_detail), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			List<String> titles = new ArrayList<String>(programs.size());

			for (Program prog : programs) {
				titles.add(prog.getTitle());
			}

			setListAdapter(new FavoriteProgramAdapter(
					FavoriteProgramActivity.this, R.layout.program_list_item,
					programs));

			pd.dismiss();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.program_list);
		loadFavoriteProgram();
	}
	
	private void loadFavoriteProgram() {
		LoadFavoriteProgramTask task = new LoadFavoriteProgramTask();
		task.execute(new Void[] {});
	}

}
